export function base64EncodeUnicode(str) {
  // First we escape the string using encodeURIComponent to get the UTF-8 encoding of the characters,
  // then we convert the percent encodings into raw bytes, and finally feed it to btoa() function.
  var utf8Bytes = encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(
    match,
    p1
  ) {
    return String.fromCharCode("0x" + p1);
  });
  return btoa(utf8Bytes);
}
export function base64DecodeUnicode(str) {
  // Going backwards: from bytestream, to percent-encoding, to original string.
  return decodeURIComponent(
    atob(str)
      .split("")
      .map(function(c) {
        return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
      })
      .join("")
  );
}
export function i2s(i) {
  var s = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
  var sArray = s.split(" ");
  if (i < 1) return "";

  if (parseInt(i / 26 + "") == 0) {
    return sArray[(i % 26) - 1];
  } else {
    if (i % 26 == 0) {
      return i2s(parseInt(i / 26 + "") - 1) + sArray[26 - 1];
    } else {
      return sArray[parseInt(i / 26 + "") - 1] + sArray[(i % 26) - 1];
    }
  }
}
// 参数序列化
export function serialize(obj) {
  var str = "";
  obj.forEach((item, i) => {
    str = str + `idList[]=${item.idList}&`;
  });
  return str.substring(0, str.length - 1);
}
// 判断对象参数是否全部为空
export const paramsValidate = function(obj) {
  let empty = null;
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (obj[key] === null || obj[key] === "") {
        empty = true;
      } else {
        empty = false;
        break;
      }
    }
  }
  return empty;
};

export function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
    default:
      return 0;
  }
}

export function is(val, type) {
  return toString.call(val) === `[object ${type}]`;
}

// 10进制转rgba
export function decToRgba(number) {
  const blue = number & 0xff;
  const green = (number >> 8) & 0xff;
  const red = (number >> 16) & 0xff;
  number = (number >> 24) & 0xff;
  const alpha = (number / 0xff).toFixed(2);
  return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
}
export const hex2Rgba = (bgColor, alpha = 1) => {
  let color = bgColor.slice(1); // 去掉'#'号
  let rgba = [
    parseInt("0x" + color.slice(0, 2)),
    parseInt("0x" + color.slice(2, 4)),
    parseInt("0x" + color.slice(4, 6)),
    alpha
  ];
  return "rgba(" + rgba.toString() + ")";
};
